<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Holder of all the Model implementation for MVC 
 *
 * PHP versions 5
 *
 * LICENSE: This source file is subject to version 3.0 of the PHP license
 * that is available through the world-wide-web at the following URI:
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
 * the PHP License and are unable to obtain it through the web, please
 * send a note to license@php.net so we can mail you a copy immediately.
 *
 * @category   Model 
 * @package    DINAMIC
 * @author     Kanazawa Kazuyoshi <kazu.php@gmail.com>
 * @copyright
 * @license
 * @version    CVS: $Id: model.class.php 69 2008-02-17 10:22:54Z kazu.php $
 * @link
 * @see
 * @since
 * @deprecated
 */

/**
 * PHP versions 5
 *
 * LICENSE: This source file is subject to version 3.0 of the PHP license
 * that is available through the world-wide-web at the following URI:
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
 * the PHP License and are unable to obtain it through the web, please
 * send a note to license@php.net so we can mail you a copy immediately.
 *
 *
 * @category   Model 
 * @package    DINAMIC
 * @author     Kanazawa Kazuyoshi <kazu.php@gmail.com>
 * @copyright
 * @license
 * @version    CVS: $Id: model.class.php 69 2008-02-17 10:22:54Z kazu.php $
 * @link
 * @see
 * @since
 * @deprecated
 */
class model 
{
    public      $errors     = array();
    protected   $aggregated = array();
    protected   $results    = array();

    // {{{ __construct

    public function __construct()
    {
    }

    // }}}
    // {{{ exists

    public static function exists($model)
    {
        $model_filename = 
            config::get('mvc.models.folder') .
            $model . 
            config::get('php.file.class.extension');
        return file_exists($model_filename);
    }

    // }}}
    // {{{ addErrors

    public function addErrors($id, $value)
    {
        $this->errors[$id] = $value;
    } 

    // }}}
    // {{{ getErrors

    public function getErrors()
    {
        return $this->errors;
    }

    // }}}
    // {{{ execute 

    /**
     * The model implementation should be done here when extended, other tools
     * and help-functions are defined here to make this easier
     *
     * <code>
     *
     *
     * $result = array(
     *     array(
     *         'id' => 1,
     *         'name' => 'john',
     *         'age' => 23
     *     ), 
     *     array(
     *         'id' => 2,
     *         'name' => 'michael',
     *         'age' => 29
     *     ), 
     * );
     *
     *  // imagine that $result came from a db-query
     *  // and you want to bind that data with your template.
     *
     * // if your template looks like this 
     * //
     * //  $name is $age
     * //
     *
     * // you'll get this output 
     * //
     * //  john is 23
     * //  michael is 29 
     * //
     *
     * </code>
     * @access public
     */
    public function execute()
    {
     // $class = $this->template->model . 
     //     config::DAO_CLASS_SUFFIX;
     // $filepath = config::PATH_DAOS . $class . 
     //     config::FILE_PHP_CLASS_EXT;

     // if (file_exists($filepath)) {
     //     $dao = new $class; 
     //     $dao->changeTablename($this->template->model);
     //     $this->show($dao->records());
     // } else {
     //     $this->show(array());
     // }
    }

    // }}}

    public function add($param, $value)
    {
        $this->append($param, $value);
    }

    public function addArray($array, $override = NULL)
    {
        foreach ($array as $param => $value) {
            if (is_array($value)) {
                $this->addArray($value, $override);
            } else {
                $this->append($param, $value);
            }
        }
    }

    /**
     * addPacket 
     * 
     * @param mixed $array 
     * @access  public
     * @return  void
     */
    public function addPacket($array)
    {
        $this->results[] = $array;
    }

    public function getResults()
    {
        return $this->results;
    }

    public function wasAdded($param)
    {
        return in_array($param, $this->aggregated);
    }

    private function append($param, $value)
    {
        $object = new stdClass;
        $object->param = $param;
        $object->value = $value;
        $this->results[] = $object;
        if (!in_array($param, $this->aggregated)) {
            $this->aggregated[] = $param;
        }
    }

}

?>
